package com.ws.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.commonutils.JwtUtils;
import com.ws.commonutils.R;
import com.ws.eduservice.client.OrderClient;
import com.ws.eduservice.entity.EduCourse;
import com.ws.eduservice.entity.chapter.ChapterVo;
import com.ws.eduservice.frontvo.CourseFrontVo;
import com.ws.eduservice.frontvo.CourseWebVo;
import com.ws.eduservice.service.EduChapterService;
import com.ws.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ws
 * @create 2024-12-13 09:43
 */
@RestController
@RequestMapping("/eduservice/frontcourse")
@CrossOrigin

public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @PostMapping("getPageList/{current}/{limit}")
    public R getPageList(@PathVariable Long current, @PathVariable Long limit,
                         @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> page = new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        courseService.page(page,wrapper);
        List<EduCourse> eduCourseList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("list",eduCourseList);
    }

    @GetMapping("getCouresInfo/{id}")
    public R getCouresInfo(@PathVariable String id, HttpServletRequest request) {
        CourseWebVo courseWebVo=courseService.getCourseFront(id);

        List<ChapterVo> chapterVoList = chapterService.getAddChaperList(id);

        Boolean isBuy = orderClient.isBuy(id, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("isBuy",isBuy);
    }

    @GetMapping("getOrderCourse/{courseId}")
    public com.ws.commonutils.CourseWebVo getOrderCourse(@PathVariable String courseId) {
        CourseWebVo courseFront = courseService.getCourseFront(courseId);
        com.ws.commonutils.CourseWebVo courseWebVo = new com.ws.commonutils.CourseWebVo();
        BeanUtils.copyProperties(courseFront,courseWebVo);
        return courseWebVo;

    }

}
